package com.oracle.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.common.PageUtil;
import com.oracle.entity.Task;
import com.oracle.mapper.TaskMapper;
import com.oracle.service.TaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oracle.vo.req.TaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2025年05月15日
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Override
    public List<Task> findAll() {
          return taskMapper.selectList(null);
    }

    @Override
    public PageUtil findPage(Integer pageNo, Integer pageSize) {
        //1、装配页码
        PageHelper.startPage(pageNo,pageSize);
        //2、数据查询 检索
        Page<Task> tasksPage = (Page<Task>) taskMapper.selectList(null);

        return new PageUtil(tasksPage.getTotal(),tasksPage.getResult());
    }

    @Override
    public PageUtil search(TaskVo taskVo, Integer pageNo, Integer pageSize) {
        //1、装配页码
        PageHelper.startPage(pageNo,pageSize);

        //检索条件参数
        QueryWrapper queryWrapper = new QueryWrapper();

        //1.1 校验检索参数
        //首次访问 条件参数一定是null
        if(!ObjectUtils.isEmpty(taskVo)){
            if(!StringUtils.isEmpty(taskVo.getStartTime())){
                queryWrapper.gt("lauchtime",taskVo.getStartTime());
            }

            if(!StringUtils.isEmpty(taskVo.getEndTime())){
                queryWrapper.lt("lauchtime",taskVo.getEndTime());
            }

            if(!ObjectUtils.isEmpty(taskVo.getTypeid())){
                queryWrapper.eq("typeid",taskVo.getTypeid());
            }

            if(!ObjectUtils.isEmpty(taskVo.getState())){
                queryWrapper.eq("state",taskVo.getState());
            }
        }

        //2、数据查询 检索
        Page<Task> tasksPage = (Page<Task>) taskMapper.selectList(queryWrapper);

        return new PageUtil(tasksPage.getTotal(),tasksPage.getResult());
    }

    @Override
    public void updateTaskStatus(Task task) {

        //1、查询
        Task result = taskMapper.selectById(task.getId());
        result.setState(task.getState());

        //2、修改
        taskMapper.updateById(task);

    }

    @Override
    public Task findTaskById(Integer id) {
        return taskMapper.selectById(id);
    }
}
